/* Copyright (c) [2025] [zhi1208@126.com]
 * SPDX-License-Identifier: MIT
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "hmi_list_item.h"
#include "hmi_tinyttf.h"
#include "hmi_png.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void item_generic_ev_cb(lv_event_t *e);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
lv_obj_t *hmi_list_item_create(lv_obj_t *parent, const hmi_list_item_cfg_t *cfg)
{
    lv_obj_t *item = lv_obj_create(parent);
    lv_obj_remove_style_all(item);
    lv_obj_set_size(item, lv_pct(100), 60);
    lv_obj_set_style_pad_column(item, 20, 0);
    lv_obj_set_flex_flow(item, LV_FLEX_FLOW_ROW);
    lv_obj_set_style_bg_color(item, lv_palette_main(LV_PALETTE_GREY), 0);
    lv_obj_set_style_radius(item, 10, 0);
    lv_obj_set_style_pad_hor(item, 10, 0);

    if (cfg->icon)
    {
        lv_obj_t *icon_wrap = lv_obj_create(item);
        lv_obj_remove_style_all(icon_wrap);
        lv_obj_set_height(icon_wrap, lv_pct(100));
        lv_obj_set_style_radius(icon_wrap, LV_RADIUS_CIRCLE, 0);
        lv_obj_set_style_bg_opa(icon_wrap, LV_OPA_COVER, 0);
        lv_obj_set_style_bg_color(icon_wrap, cfg->color, 0);
        lv_obj_update_layout(icon_wrap);
        lv_obj_set_width(icon_wrap, lv_obj_get_height(icon_wrap));
        lv_obj_add_flag(icon_wrap, LV_OBJ_FLAG_EVENT_BUBBLE);

        lv_obj_t *icon = lv_image_create(icon_wrap);
        lv_obj_center(icon);
        lv_image_set_src(icon, cfg->icon);
        lv_obj_add_flag(icon, LV_OBJ_FLAG_EVENT_BUBBLE);
    }

    lv_obj_t *cont = lv_obj_create(item);
    lv_obj_remove_style_all(cont);
    lv_obj_set_flex_grow(cont, 1);
    lv_obj_set_height(cont, lv_pct(100));
    lv_obj_set_style_border_side(cont, LV_BORDER_SIDE_BOTTOM, 0);
    lv_obj_set_style_border_width(cont, 2, 0);
    lv_obj_set_style_border_color(cont, lv_palette_main(LV_PALETTE_GREY), 0);
    lv_obj_set_style_border_opa(cont, LV_OPA_COVER, 0);
    lv_obj_set_grid_align(cont, LV_GRID_ALIGN_SPACE_EVENLY, LV_GRID_ALIGN_SPACE_EVENLY);
    lv_obj_add_flag(cont, LV_OBJ_FLAG_EVENT_BUBBLE);

    static int32_t col_dsc_default[] = {LV_GRID_FR(6), LV_GRID_FR(6), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
    static int32_t col_dsc_without_chevron[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
    static int32_t row_dsc[] = {LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};

    if (cfg->click_cb)
        lv_obj_set_grid_dsc_array(cont, col_dsc_default, row_dsc);
    else
        lv_obj_set_grid_dsc_array(cont, col_dsc_without_chevron, row_dsc);

    static lv_font_t *title_font;
    if (!title_font)
        title_font = hmi_tiny_ttf_create_static("def_font", 24, 0);

    lv_obj_t *title = lv_label_create(cont);
    lv_obj_set_style_text_font(title, title_font, 0);
    lv_obj_set_style_text_color(title, lv_color_black(), 0);
    lv_label_set_text(title, cfg->title);
    lv_label_set_long_mode(title, LV_LABEL_LONG_MODE_SCROLL);
    lv_obj_set_grid_cell(title, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    lv_obj_add_flag(title, LV_OBJ_FLAG_EVENT_BUBBLE);

    if (cfg->info)
    {
        lv_obj_set_parent(cfg->info, cont);
        lv_obj_set_grid_cell(cfg->info, LV_GRID_ALIGN_END, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    }

    if (cfg->click_cb)
    {
        lv_obj_t *chevron = lv_image_create(cont);
        lv_obj_set_style_image_recolor_opa(chevron, LV_OPA_COVER, 0);
        lv_obj_set_style_image_recolor(chevron, lv_color_black(), 0);
        lv_image_set_src(chevron, hmi_png("chevron_right"));
        lv_obj_set_grid_cell(chevron, LV_GRID_ALIGN_END, 2, 1, LV_GRID_ALIGN_CENTER, 0, 1);
        lv_obj_add_flag(chevron, LV_OBJ_FLAG_EVENT_BUBBLE);

        lv_obj_add_event_cb(item, cfg->click_cb, LV_EVENT_CLICKED, cfg->click_cb_user_data);
        lv_obj_add_event_cb(item, item_generic_ev_cb, LV_EVENT_ALL, NULL);
    }

    return item;
}

char *hmi_list_item_get_title(lv_obj_t *item)
{
    lv_obj_t *cont = lv_obj_get_child(item, -1);
    lv_obj_t *title = lv_obj_get_child(cont, 0);
    return lv_label_get_text(title);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void item_generic_ev_cb(lv_event_t *e)
{
    lv_obj_t *item = lv_event_get_current_target_obj(e);

    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_PRESSING)
        lv_obj_set_style_bg_opa(item, LV_OPA_50, 0);
    else if (code == LV_EVENT_RELEASED)
        lv_obj_set_style_bg_opa(item, LV_OPA_TRANSP, 0);
}